<?php


$cedula = intval($_POST['element_1']);

if ($cedula != 0) {


    $DB_NAME = 'tesis';
    $DB_HOST = 'localhost';
    $DB_USER = 'gach';
    $DB_PASS = 'gach18942bakuryu';

    $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

// se checkea la información de login
    $query = "CALL login($cedula,@x);select @x";
    // $query2= "SELECT final_encuesta from TESIS.ALUMNO WHERE idalumno=$cedula and final_encuesta=0";
    $res = $mysqli->multi_query($query) or die($mysqli->error . __LINE__);
    // $res2= $mysqli->multi_query($query2) or die($mysqli->error . __LINE__);
// GOING THROUGH THE DATA


    if ($res) {

        do {
            if ($result = $mysqli->store_result()) {
                while ($row = $result->fetch_row()) {
                    foreach ($row as $cell)
                        ;
                    $tipousuario = intval($cell);
                }
                $result->close();
            }
        } while ($mysqli->next_result());
    }





    if (tipousuario == 0) {
        header("Location: http://gach-laptop.no-ip.org/login/login.php");
    }

    if ($tipousuario == 1) {
        $query = "CALL buscar_materias_alumno($cedula)";
        $res = $mysqli->multi_query($query) or die($mysqli->error . __LINE__);
        $materias = array();
        if ($res) {

            do {
                if ($result = $mysqli->store_result()) {
                    while ($row = $result->fetch_row()) {
                        array_push($materias, $row);

                        ;
                    }
                    $result->close();
                }
            } while ($mysqli->next_result());
        }

        if (!empty($materias)) {


            $materia = $materias[0][1];
            $seccion = $materias[0][0];
            $nombre = $materias [0][2];
            $apellido = $materias[0][3];
            header("Location: http://gach-laptop.no-ip.org/encuesta/encuesta.php?materia=" . $materia . "&seccion=" . $seccion . "&cedula=" . $cedula . "&nombre=" . $nombre . "&apellido=" . $apellido . ""); //redireccionamiento a la proxima pagina
        } else {
            header("Location: http://gach-laptop.no-ip.org/final/no_encuesta.php");
        }
        $mysqli->close();
    }

    if ($tipousuario == 2) {

        $query = "SELECT nombreprofesor,apellidoprofesor FROM tesis.materia WHERE idprofesor='$cedula' AND statusmateria=0";
        $res = $mysqli->multi_query($query) or die($mysqli->error . __LINE__);
        $materias = array();
        if ($res) {

            do {
                if ($result = $mysqli->store_result()) {
                    while ($row = $result->fetch_row()) {
                        array_push($materias, $row);

                        ;
                    }
                    $result->close();
                }
            } while ($mysqli->next_result());
        }
        if (!empty($materias)) {
            $nombre = $materias [0][0];
            $apellido = $materias[0][1];

            header("Location: http://gach-laptop.no-ip.org/materias_disponibles/materias_disponibles.php?cedula=" . $cedula . "&nombre=" . $nombre . "&apellido=" . $apellido . "");
            $mysqli->close();
        } else {
            header("Location: http://gach-laptop.no-ip.org/final/no_activacion.php");
            $mysqli->close();
        }
    }
    if ($tipousuario == 3) {
        $query = "SELECT nombreadministrador,apellidoadministrador FROM tesis.administrador WHERE idadministrador='$cedula'";
        $res = $mysqli->multi_query($query) or die($mysqli->error . __LINE__);
        $info = array();
        if ($res) {

            do {
                if ($result = $mysqli->store_result()) {
                    while ($row = $result->fetch_row()) {
                        array_push($info, $row);

                        ;
                    }
                    $result->close();
                }
            } while ($mysqli->next_result());
        }
         $nombre = $info [0][0];
         $apellido = $info[0][1];
        header("Location: http://gach-laptop.no-ip.org/admin/admin.php?cedula=" . $cedula. "&nombre=" . $nombre . "&apellido=" . $apellido . "");


        $mysqli->close();
    }
} else {

    header("Location: http://gach-laptop.no-ip.org/login/login.php");
}
?>
